﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tzync
{
    public partial class FiltersTreeViewUIForm : Form
    {
        string substringDirectory;
        private List<string> treeStorage = new List<string>(), returnList = new List<string>();
        private SyncTool synctool;

        public FiltersTreeViewUIForm(SyncTool syncTool,ref List<string> returnList)
        {
            InitializeComponent();

            this.returnList = returnList;
            this.synctool = syncTool;
            DirectoryTree.CheckBoxes = true;
            DirectoryTree.Nodes.Clear();

            String path = syncTool.GetRootAddress() ;

            DirectoryTree.Nodes.Add(path);
            treeStorage.Add(path +"|0|0|0");
            PopulateTreeView(path, DirectoryTree.Nodes[0]);
        }

        private void FiltersTreeViewUIForm_Load(object sender, EventArgs e)
        {
            addBtnToolTip.SetToolTip(addBtn, "Click here to add the selected file or folder to the list of filters");
            cancelBtnToolTip.SetToolTip(cancelBtn, "Click here to cancel and return to the previous screen");
        }
        
        public void PopulateTreeView(string directoryValue, System.Windows.Forms.TreeNode parentNode)
        {
            string[] directoryArray =
            Directory.GetDirectories(directoryValue);
            string[] fileArray = Directory.GetFiles(directoryValue);
            foreach (string file in fileArray)
            {
                substringDirectory = file.Substring(
                    file.LastIndexOf('\\') + 1,
                    file.Length - file.LastIndexOf('\\') - 1);
                    System.Windows.Forms.TreeNode fileNode = new System.Windows.Forms.TreeNode(substringDirectory);

                    parentNode.Nodes.Add(fileNode);
                    treeStorage.Add(file + "|" + fileNode.Level + "|" + fileNode.Parent.Text+"|"+fileNode.Index);

            }
            try
            {
                if (directoryArray.Length != 0)
                {
                    foreach (string directory in directoryArray)
                    {
                        
                        substringDirectory = directory.Substring(
                        directory.LastIndexOf('\\') + 1,
                        directory.Length - directory.LastIndexOf('\\') - 1);

                        System.Windows.Forms.TreeNode myNode = new System.Windows.Forms.TreeNode(substringDirectory);

                        parentNode.Nodes.Add(myNode);
                        treeStorage.Add(directory.ToString() + "|" + myNode.Level + "|" + myNode.Parent.Text + "|" + myNode.Index);

                        PopulateTreeView(directory, myNode);
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                parentNode.Nodes.Add("Access denied");
            } // end catch
        }

        private void AdvancedFilters_Btn_Click(object sender, EventArgs e)
        {
          CallRecursive();
            
            this.Close();
        }

        private void TraverseRecursive(System.Windows.Forms.TreeNode treeNode)
        {
            bool flag = false;
            // check
            if (treeNode.Checked == true &&  flag==false)
            {
                foreach (string val in treeStorage)
                {
                    string[] arrWords;
                    arrWords = val.Split('|');
                    if(treeNode.Parent==null)
                    {
                        returnList.Add(synctool.GetRootAddress());
                        flag = true;
                        break;
                    }
                    if(int.Parse(arrWords[1])==treeNode.Level && arrWords[2]==treeNode.Parent.Text && int.Parse(arrWords[3])==treeNode.Index)
                    {
                        returnList.Add(arrWords[0]);
                        break;
                    }
   
                }
            }

            // Print each node recursively.
            foreach (System.Windows.Forms.TreeNode tn in treeNode.Nodes)
            {
                TraverseRecursive(tn);
            }
        }

        // Call the procedure using the TreeView.
        private void CallRecursive()
        {
            // Print each node recursively.
            TreeNodeCollection nodes = DirectoryTree.Nodes;
            foreach (System.Windows.Forms.TreeNode n in nodes)
            {
                TraverseRecursive(n);
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            CallRecursive();
            this.Close();
        }
    }
}
